﻿Imports FixSoft.Fixed.Forms
Imports System.Collections.Generic
Imports System.Reflection

Public Class BackgroundForm
    Dim WithEvents ForeFormv As HyperAero = New ForeForm
    Public Property ForeForm As HyperAero
        Get
            Return ForeFormv
        End Get
        Set(ByVal value As HyperAero)
            ForeFormv = value
        End Set
    End Property

    Public Overrides Property CaptionBarRenderMode As FixSoft.Fixed.Forms.CaptionBarStates
        Get
            If ActiveForm Is ForeForm OrElse ActiveForm Is Me Then
                Return CaptionBarStates.Active
            Else
                Return CaptionBarStates.AutoSet
            End If
        End Get
        Set(ByVal value As FixSoft.Fixed.Forms.CaptionBarStates)
            ' MyBase.CaptionBarRenderMode = value
        End Set
    End Property


    Private Sub BackgroundForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        UpdateMe()
        ForeForm.TopLevel = True
        ForeForm.StartAnimationMode = AnimationMode.Fade
        ForeForm.SpeedOfFade = 5
        ForeForm.SpeedOfFade2 = 0.035
        ForeForm.TransparencyKey = ForeForm.BackColor
        ForeForm.AeroBackgroundEnabled = False
        ForeForm.Show(Me)
    End Sub
    ''' <summary>
    ''' Sets properties with ForeForm (Like icon,text,location,etc).
    ''' </summary>
    Public Sub UpdateMe()
        If False Then
            Dim NF As New Form
            Dim BaseFormProperties As New List(Of PropertyInfo)(NF.GetType.GetProperties)
            Dim BaseFormNames As New List(Of String)
            For Each Item In BaseFormProperties
                BaseFormNames.Add(Item.Name)
            Next
            For Each Propertry In ForeForm.GetType.GetProperties()
                For Each Item In BaseFormNames
                    If Propertry.Name = Item Then
                        Continue For
                    End If
                Next
                Select Case Propertry.Name
                    Case "AeroBackgroundEnabled", "StartAnimationMode", "SpeedOfFade", "SpeedOfFade2"   ', "FormBorderStyle", "TransparencyKey", "Parent", "Owner", "ShowInTaskbar", "Handle", "HasChildren", "OwnedForms", "Controls", "Focused"
                        Continue For
                End Select
                If Propertry.CanWrite AndAlso Propertry.CanRead Then
                    Propertry.SetValue(Me, Propertry.GetValue(ForeForm, Nothing), Nothing)
                End If
            Next
        End If

        Me.Location = New Point(ForeForm.Location.X + 4, ForeForm.Location.Y + 4)
        Me.RealTop = ForeForm.RealTop
        Dim Fs As New Size(ForeForm.Size.Width - 8, ForeForm.Size.Height - 8)
        If Fs.Height < PreferredSize.Height OrElse Fs.Width < PreferredSize.Width Then
            ForeForm.Size = New Size(Me.Size.Width + 8, Me.Size.Height + 8)
        Else
            Me.Size = Fs
        End If

        Me.Text = ForeForm.Text
        Me.RealIcon = ForeForm.Icon
        Me.StartPosition = ForeForm.StartPosition
        Me.StartLocation = ForeForm.StartLocation
        Me.WindowState = ForeForm.WindowState
        Me.ForeColor = ForeForm.ForeColor
        Me.Font = ForeForm.Font
        Me.GradientMode = ForeForm.GradientMode
    End Sub
    Private Sub SetHelper(ByVal PropertyName As String, ByVal Value As Object)
        CallByName(ForeForm, PropertyName, CallType.Set, Value)
    End Sub

    Private Sub ForeFormv_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles ForeFormv.Activated, ForeFormv.Deactivate
        AeroAPIs.SendMessage(Handle.ToInt32, AeroAPIs.WM_NCACTIVATE, CaptionBarRenderMode, 0)
    End Sub
    Private Sub ForeFormv_LocationChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ForeFormv.LocationChanged
        'Static IsCalled As Boolean
        'If IsCalled = True Then
        '  IsCalled = False
        '   Exit Sub
        'End If
        ' IsCalled = True
        ' IsCalledLocationChanged = True
        Me.Location = New Point(ForeForm.Location.X + 4, ForeForm.Location.Y + 4)
    End Sub

    Private Sub BackgroundForm_LocationChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.LocationChanged
        ForeForm.Location = New Point(Me.Location.X - 4, Me.Location.Y - 4)
    End Sub

    Private Sub BackgroundForm_SizeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.SizeChanged
        ForeForm.Size = New Size(Me.Size.Width + 8, Me.Size.Height + 8)
    End Sub

    Private Sub ForeFormv_SizeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ForeFormv.SizeChanged
        Me.Size = New Size(ForeForm.Size.Width - 8, ForeForm.Size.Height - 8)
    End Sub
End Class
